package cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.ui;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.mifengkong.frtools.util.FRString;

import java.util.ArrayList;
import java.util.List;

import cn.mifengkong.appcommon.util.ActivityStartUtil;
import cn.rongdao.jrkabao.BuildConfig;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.HomeBean;
import cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.adapter.CashFunctionAdapter;
import cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.adapter.NavigationViewPagerAdapter;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.LoginUtil;
import cn.rongdao.jrkabao.widget.IndicatorView;

/**
 * Created by WANG on 17/12/19
 */

public class NavigationComponent extends LinearLayout {

    public NavigationComponent(Context context) {
        super(context);
    }

    public NavigationComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public NavigationComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private ViewPager mViewPager;
    private IndicatorView mDotContainer;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mViewPager = findViewById(R.id.lhn_viewpager);
        mDotContainer = findViewById(R.id.lhn_home_indicator);
    }

    public void setDataList(final List<HomeBean.BlockListBean> dataList) {
        //一共的页数等于 总数/每页数量，并取整
        int pageCount = (int) Math.ceil(dataList.size() * 1.0 / 4);
        List<View> viewList = new ArrayList<>();
        for (int index = 0; index < pageCount; index++) {
            //每个页面都是inflate出一个新实例
            RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(getContext()).inflate(R.layout.item_home_recyclerview, mViewPager, false);
            recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
            CashFunctionAdapter entranceAdapter = new CashFunctionAdapter(getContext(), dataList, index);
            entranceAdapter.setOnCashFunctionAdapterClickListener(new CashFunctionAdapter.OnCashFunctionAdapterClickListener() {
                @Override
                public void onClick(View view, int position) {
                    HomeBean.BlockListBean indexModelBean = dataList.get(position);
                    String need_login = indexModelBean.getNeed_login();
                    if (!LoginUtil.isLogin() && CommonUtil.isStatusSuccess(need_login)) {
                        LoginUtil.isLogin(getContext());
                        return;
                    }
                    String url = indexModelBean.getUrl();
                    if (!FRString.isEmpty(url)) {
                        if (url.startsWith("kamao://")) {
                            ActivityStartUtil.launch(getContext(), BuildConfig.APPLICATION_ID, url);
                        } else {
                            WebViewActivity.start(getContext(), url);
                        }
                    }
                }
            });
            recyclerView.setAdapter(entranceAdapter);
            viewList.add(recyclerView);
        }
        NavigationViewPagerAdapter pagerAdapter = new NavigationViewPagerAdapter(viewList);
        mViewPager.setAdapter(pagerAdapter);
        if (pageCount > 1) {
            mDotContainer.setVisibility(View.VISIBLE);
            mDotContainer.setIndicatorCount(mViewPager.getAdapter().getCount());
            mDotContainer.setCurrentIndicator(mViewPager.getCurrentItem());
            mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    mDotContainer.setCurrentIndicator(position);
                }
            });
        } else {
            mDotContainer.setVisibility(View.GONE);
        }
    }
}